package oop.factor;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;


import oop.data.DataStore;
import oop.data.RowData;


//处理文件的输出
public class Outer{
	private Map<String,DataStore<RowData>>  datas = new LinkedHashMap<>();
	
	public Outer(Map<String,DataStore<RowData>> datas){
		this.datas = datas;

		try {
			toSource();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			toResult();
		} catch (IOException e) {
			e.printStackTrace();
		}



	}
	//修改原始文件
	public void toSource()throws IOException{
		String currentDir = System.getProperty("user.dir");

		try{
			//在result目录下重写原文件
			for(DataStore<RowData> value : datas.values()){
				Iterator<RowData> iterator = value.getIterator();
				while(iterator.hasNext()){
					RowData attribute =  iterator.next();
					//输出目录路径
					String fileName =  System.getProperty("user.dir") + "/OOP-Experiment-2/result/" + attribute.getFatherName() + ".csv";
					File outFile = new File(fileName);

					//不存在则创建，且写入头
					if(!outFile.exists()){
						FileWriter fileWritter = new FileWriter(fileName,true);
						BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
				        String header = "Date,Open,High,Low,Close,Adj close,Volumn\n";
						bufferWritter.write(header);
						bufferWritter.close();
					}
					//分行写入
					String fileData = "";
					for(String column : attribute.getColumn() ){
						fileData += attribute.getValue(column) + ","; 
					}
					fileData = fileData.substring(0,fileData.length() - 1) + "\n";
					FileWriter fileWritter = new FileWriter(fileName,true);
					BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
					bufferWritter.write(fileData);
					bufferWritter.close();

				}
				
			}

		}
		catch (IOException e) {
			   e.printStackTrace();
			  }
	}


    //输出统计后文件
	public void toResult() throws IOException{
		String currentDir = System.getProperty("user.dir");

		try{
			//输出目录路径

			String fileName =  System.getProperty("user.dir") + "/OOP-Experiment-2/result/result.csv" ;

			File file = new File(fileName);
			if(!file.exists()){
				//写文件头
				FileWriter fileWritter = new FileWriter(fileName,true);
				BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
				String header = "date,features,BA,CAT,DIS,GE,IBM";
				bufferWritter.write(header);
				bufferWritter.close();
			}
			//遍历map
			Map<String,String[][]> writerMap = getMatrix();
			for(String date : writerMap.keySet()){
				String writer ;
				writer = "\n" + date + ",";
				String[][] matrix = writerMap.get(date);
				for(int x = 0;x < matrix.length;x++){
					for(int y = 0;y < matrix[x].length;y++){
						if(matrix[x][y] == null)
							matrix[x][y] = "\t";
						writer += matrix[x][y] + ",";
					}
					writer = writer.substring(0,writer.length() - 1) + "\n";
					FileWriter fileWritter = new FileWriter(fileName,true);
					BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
					bufferWritter.write(writer);
					bufferWritter.close();
					writer = "\t,";
				}
			}
		}
		catch (IOException e) {
			   e.printStackTrace();
			  }
	}


	//将原始map转化为 key-二维矩阵的 方便输出
	public  Map<String,String[][]>  getMatrix(){
		Map<String,String[][]> dateMap = new LinkedHashMap<>();
		//文件名索引
		Map<String, Integer> featureMap = new LinkedHashMap<String, Integer>() {{
			put( "BA" , 1 );
			put( "CAT", 2 );
			put( "DIS", 3 );
			put( "GE", 4 );
			put( "IBM", 5 );

		}};
		//遍历数据Map
		for(String date : datas.keySet()){
			String[][] matrix = new String[6][6];
			Iterator<RowData> iterator = datas.get(date).getIterator();
			//同一日期下Row数据
			while (iterator.hasNext()){
				int count = 0;
				RowData rowData = iterator.next();
				//写入矩阵值
				int column = featureMap.get(rowData.getFatherName());
				for(String features : rowData.getColumn())
				{
					//忽略date
					if(features.equals("Date")){
						continue;
					}
					matrix[count][0] = features;
					matrix[count][column] = rowData.getValue(features);
					count++;
				}
			}
			dateMap.put(date,matrix);
		}
		return dateMap;
	}

}
